import { NetWorkService } from "../../apiNetworkServices"

export type BodyState = {
  id?: string
  body?: {
    clients?: []
    end_time?: string
    start_date?: string
    selected_dates?: []
    select_dates?: boolean
    weekly_repeat?: boolean
    number_of_weeks?: string
    appointment_services?: []
    weekly_repeat_gap?: string
    provider_location?: string
  }
}

export type BookingState = {
  body?: {
    start_time?: string
    end_time?: string
    start_date?: string
    selected_dates?: []
    select_dates?: boolean
    room?: string
  }
}
/**
 * Network request used to create new appointment without ethera location
 * @returns the response success or error
 */

const createAppointmentWithoutBooking = (body?: BodyState) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Post({
      url: `/api/appointment`,
      body: body,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to update existing appointment without ethera location
 * @returns the response success or error
 */

const updateAppointment = (params?: BodyState) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Patch({
      url: `/api/appointment/${params?.id}`,
      body: params?.body,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to create new appointment with ethera location
 * @returns the response success or error
 */

const createAppointmentWithBooking = (params?: any) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Post({
      url: `/api/booking/${params?.id}/appointment`,
      body: params?.body,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to create new booking with ethera location
 * @returns the response success or error
 */

const createBooking = (body: BookingState) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Post({
      url: `/api/booking`,
      body,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to update existing appointment with ethera location
 * @returns the response success or error
 */

const updateAppointmentBooking = (params?: BodyState) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Patch({
      url: `/api/booking/${params?.id}`,
      body: params?.body,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to delete  booking
 * @returns the response success or error
 */

const deleteAppointmentBooking = (bookingId: string) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Delete({
      url: `/api/booking/${bookingId}`,
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to cancel existing appointment
 * @returns the response success or error
 */

const cancelAppointment = (id?: string) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Patch({
      url: `/api/appointment/${id}`,
      body: { status: 2 },
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

/**
 * Network request used to cancel booking for existing appointment
 * @returns the response success or error
 */

const cancelAppointmentBooking = (id: string) => {
  return new Promise((resolve, reject) => {
    NetWorkService.Patch({
      url: `api/booking/${id}`,
      body: { status: 2 },
    })
      .then((res) => {
        resolve(res)
      })
      .catch((error) => reject(error))
  })
}

export {
  createBooking,
  updateAppointment,
  cancelAppointment,
  deleteAppointmentBooking,
  cancelAppointmentBooking,
  updateAppointmentBooking,
  createAppointmentWithBooking,
  createAppointmentWithoutBooking,
}
